package com.e.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import db.DBManager;
import db.StudentBean;
import db.QueryDataResult;

/**
 * Servlet implementation class ZhangSanServlet
 */
public class HeroServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HeroServlet() {
        super();
        // TODO Auto-generated constructor stub
    }


	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());

		String sex=request.getParameter("sex");
		String className=request.getParameter("className");
		
		ArrayList<StudentBean> aList=new ArrayList<StudentBean>();
		
		QueryDataResult qResult =DBManager.excuteQuery("select sId,sex,sname,className from stu where sex='"+sex+"' and className='"+className+"'");
		if (qResult!=null) {
			try {
		    	ResultSet rsSet=qResult.rs;
		    	while (rsSet.next()) {
		    		//System.out.println(rsSet.getString("sname"));
		    		//System.out.println(rsSet.getString("className"));
		    		aList.add(new StudentBean(rsSet.getInt("sId"),rsSet.getString("sname"),rsSet.getString("sex"),
							rsSet.getString("className")));
				}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		    qResult.close();
		}
	    
		
    	response.setCharacterEncoding("UTF-8");//设置输出内容的编码格式
		response.setContentType("text/html");//设置输出的文件类型
		PrintWriter out = response.getWriter();//响应页面
		out.println("<table><tr><td>id</td><td>姓名</td><td>性别</td><td>班级</td>");
		for (StudentBean studentBean : aList) {
			out.println("<table><tr><td>"+studentBean.getsId()+"</td><td>"+studentBean.getSname()+"</td><td>"+studentBean.getSex()+
					"</td><td>"+studentBean.getClassName()+"</td>");
		}
		out.flush();
		out.close();// 关闭输出流
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}

}
